<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Statistics extends MY_Controller {

	private $points_perfect;
	private $points_difference;
	private $points_tendency;
	
	private $count_user;
	private $count_tipps;
	private $count_results;

function Statistics() {

	parent::MY_Controller();

	$this->load->plugin('jpgraph');

	$this->ci =& get_instance();
	$this->points_perfect    = $this->ci->config->item('points_perfect');
	$this->points_difference = $this->ci->config->item('points_difference');
	$this->points_tendency   = $this->ci->config->item('points_tendency');
}

function index() {
	
	$this->fillCounts();
	
	$data = $this->getData('statistics');
	$d = array();

	$pt = $this->points_tendency;
	$pd = $this->points_difference;
	$pp = $this->points_perfect;
		
	// Gesamtpunkte pro Spieltag
	$sql = "SELECT"
		." matchgroup,"
		." sum(tendency*$pt + difference*$pd + perfect*$pp) as points,"
		." sum(tendency*$pt + difference*$pd + perfect*$pp) / ".$this->count_user." as average"
		." FROM ".$this->tblp
		." GROUP BY matchgroup"
		." ORDER BY LENGTH(matchgroup) ASC, matchgroup ASC";

	$query = $this->db->query($sql);
	$matchdaypoints = array ();
	$matchdaypoints_label = array ();
    foreach ($query->result() as $res) {
    	$matchdaypoints[] = $res->points;
    	// $matchdaypoints_label[] = $res->matchgroup.". (".$res->average.")";
		$matchdaypoints_label[] = $res->matchgroup.".";
    }
	$chartname = "matchdaypoints_".sizeof($matchdaypoints);
	$config['xTitle'] = "Spieltage";
	$config['yTitle'] = "Punkte";
	
	$d['chart_matchdaypoints'] = null;
	if (sizeof($matchdaypoints))
		$d['chart_matchdaypoints'] = barchart($chartname, $matchdaypoints, $matchdaypoints_label, $config);



	// Top Tipps/Ergebnisse
	$sql = "SELECT CONCAT(goals_h,':', goals_a) AS erg, COUNT(*) as anz,".
			 " ROUND( (COUNT(*)/(".$this->count_results."/100)) , 2) as prozent" 
			." FROM ".$this->tblm
			." WHERE goals_h IS NOT NULL"
			." GROUP BY erg ORDER BY anz DESC";
	$query = $this->db->query($sql);
    foreach ($query->result() as $res) {
    	$ergebnisse[$res->erg] = $res->prozent;
    }

	$sql = "SELECT CONCAT(g_goals_h,':', g_goals_a) AS erg, COUNT(*) as anz,".
			 " ROUND( (COUNT(*)/(".$this->count_tipps."/100)) , 2) as prozent" 
			." FROM ".$this->tblt
			." GROUP BY erg ORDER BY anz DESC LIMIT 6";
	$query = $this->db->query($sql);
    $toptipps = array();
    $toptipps_label = array();
    $topergs = array();
	foreach ($query->result() as $res) {
    	$toptipps[] = $res->prozent;
		if (isset($ergebnisse) && array_key_exists($res->erg, $ergebnisse))
    		$topergs[] = $ergebnisse[$res->erg];
    	else
    		$topergs[] = 0;
    	
    	$toptipps_label[] = $res->erg;
    }
	$chartname = "toptipps_".$this->count_tipps; 
	$config['xTitle'] = "Tipp";
	$config['yTitle'] = "Anteil in Prozent";
	if (count($toptipps) > 0) { 
		$d['chart_toptipps'] = barchart($chartname, $toptipps, $toptipps_label, $config, $topergs);
	}


	// Tordifferenzen
	$sql = "SELECT (goals_h - goals_a) AS diff, COUNT(*) as anz,".
			 " ROUND( (COUNT(*)/(".$this->count_results."/100)) , 2) as prozent" 
			." FROM ".$this->tblm
			." WHERE goals_h IS NOT NULL"
			." GROUP BY diff ORDER BY anz DESC";
	$query = $this->db->query($sql);
    foreach ($query->result() as $res) {
	   	$realediff[$res->diff] = $res->prozent;
    }

	$sql = "SELECT (g_goals_h - g_goals_a) AS diff, COUNT(*) as anz,".
			 " ROUND( (COUNT(*)/(".$this->count_tipps."/100)) , 2) as prozent" 
			." FROM ".$this->tblt
			." GROUP BY diff ORDER BY anz DESC";
	$query = $this->db->query($sql);
	$torediff = array();
    foreach ($query->result() as $res) {
	   	$torediff[] = $res->prozent;
		if (isset($realediff) &&  array_key_exists($res->diff, $realediff))
			$torediff_real[] = $realediff[$res->diff];
		else
			$torediff_real[] = 0;
    	$torediff_label[] = $res->diff;
    }
	$chartname = "torediff_".$this->count_tipps; 
	$config['xTitle'] = "Differenz";
	$config['yTitle'] = "Anteil in Prozent";
	$config['legend'] = array("I am", "legend");
	if (count($torediff)) {
		$d['chart_torediff'] = barchart($chartname, $torediff, $torediff_label, $config, $torediff_real);
	}


	$data["content"] = $this->load->view('statistics', $d, true);
	$this->load->view('layout', $data); 
}

function chart($name) {
	
	$imgloc = getChartFileLocation($name);
	$im = @ImageCreateFromPNG ($imgloc);
    if (!$im) {
        $im = ImageCreate (500, 25);
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
        ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); 
		ImageString($im, 3, 5, 5, "Error! $imgloc", $tc); 
    }

    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}


private function fillCounts() {

	// count_user
	$sql = "SELECT COUNT(*) as c FROM ".$this->tblu;
	$query = $this->db->query($sql);
	$res = $query->result();
	$this->count_user = $res[0]->c;
	
	// count_tipps
	$sql = "SELECT COUNT(*) as c FROM ".$this->tblt;
	$query = $this->db->query($sql);
	$res = $query->result();
	$this->count_tipps = $res[0]->c;
	
	// count_results
	$sql = "SELECT COUNT(*) as c FROM ".$this->tblm." WHERE goals_h IS NOT NULL";
	$query = $this->db->query($sql);
	$res = $query->result();
	$this->count_results = $res[0]->c;
}

}
?>
